# -*- coding:utf-8 -*-
#! python3

from facecat import *
import random

#重新计算最大最小值
def onCalculateChartMaxMin(chart):
	chart.candleMax = 0
	chart.candleMin = 0
	chart.volMax = 0
	chart.volMin = 0
	chart.indMin = 0
	chart.indMin = 0
	if chart.data != None and len(chart.data) > 0:
		lastValidIndex = chart.lastVisibleIndex
		if chart.lastValidIndex != -1:
			lastValidIndex = chart.lastValidIndex
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			total = chart.data[i].data1 + chart.data[i].data2 + chart.data[i].data3 + chart.data[i].data4 + chart.data[i].data5 + chart.data[i].data6
			if i == chart.firstVisibleIndex:
				chart.candleMax = total
				chart.candleMin = 0
			else:
				if (chart.candleMax < total):
					chart.candleMax = total

#绘制K线
#chart:K线
#paint:绘图对象
#clipRect:裁剪区域
def onPaintChartStock(chart, paint, clipRect):
	global drawColors
	if chart.data != None and len(chart.data) > 0:
		lastValidIndex = chart.lastVisibleIndex
		if chart.lastValidIndex != -1:
			lastValidIndex = chart.lastValidIndex
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			x = getChartX(chart, i)
			y1 = getChartY(chart, 0, chart.data[i].data1)
			zeroY =  getChartY(chart, 0, 0)
			dWidth = chart.hScalePixel / 3
			paint.fillRect(drawColors[0], x - dWidth, y1, x + dWidth, zeroY)
			y2 = getChartY(chart, 0, chart.data[i].data1 + chart.data[i].data2)
			paint.fillRect(drawColors[1], x - dWidth, y2, x + dWidth, y1)
			y3 = getChartY(chart, 0, chart.data[i].data1 + chart.data[i].data2 + chart.data[i].data3)
			paint.fillRect(drawColors[2], x - dWidth, y3, x + dWidth, y2)
			y4 = getChartY(chart, 0, chart.data[i].data1 + chart.data[i].data2 + chart.data[i].data3 + chart.data[i].data4)
			paint.fillRect(drawColors[3], x - dWidth, y4, x + dWidth, y3)
			y5 = getChartY(chart, 0, chart.data[i].data1 + chart.data[i].data2 + chart.data[i].data3 + chart.data[i].data4 + chart.data[i].data5)
			paint.fillRect(drawColors[4], x - dWidth, y5, x + dWidth, y4)
			y6 = getChartY(chart, 0, chart.data[i].data1 + chart.data[i].data2 + chart.data[i].data3 + chart.data[i].data4 + chart.data[i].data5  + chart.data[i].data6)
			paint.fillRect(drawColors[5], x - dWidth, y6, x + dWidth, y5)
			if chart.paint.defaultUIStyle == "dark":
				paint.drawRect("rgb(255,255,255)", 1, 0, x - dWidth, y6, x + dWidth, zeroY)
			elif chart.paint.defaultUIStyle == "light":
				paint.drawRect("rgb(0,0,0)", 1, 0, x - dWidth, y6, x + dWidth, zeroY)

#绘制十字线
#chart:K线
#paint:绘图对象
#clipRect:裁剪区域
def onPaintChartCrossLine(chart, paint, clipRect):
	global drawColors
	if chart.data != None and len(chart.data) > 0:
		candleDivHeight = getCandleDivHeight(chart)
		volDivHeight = getVolDivHeight(chart)
		indDivHeight = getIndDivHeight(chart)
		crossLineIndex = chart.crossStopIndex
		if crossLineIndex == -1:
			crossLineIndex = chart.lastVisibleIndex
		drawTitles = []
		drawTitles.append("项目1 " + toFixed(chart.data[crossLineIndex].data1, chart.candleDigit))
		drawTitles.append("项目2 " + toFixed(chart.data[crossLineIndex].data2, chart.candleDigit))
		drawTitles.append("项目3 " + toFixed(chart.data[crossLineIndex].data3, chart.candleDigit))
		drawTitles.append("项目4 " + toFixed(chart.data[crossLineIndex].data4, chart.candleDigit))
		drawTitles.append("项目5 " + toFixed(chart.data[crossLineIndex].data5, chart.candleDigit))
		drawTitles.append("项目6 " + toFixed(chart.data[crossLineIndex].data6, chart.candleDigit))

		iLeft = chart.leftVScaleWidth + 5
		for i in range(0, len(drawTitles)):
			tSize = paint.textSize(drawTitles[i], chart.font)
			paint.drawText(drawTitles[i], drawColors[i], chart.font, iLeft, 5 + tSize.cy / 2)
			iLeft += tSize.cx + 5

gPaint = FCPaint() #创建绘图对象
gPaint.defaultUIStyle = "dark"
gPaint.onCalculateChartMaxMin = onCalculateChartMaxMin
gPaint.onPaintChartStock = onPaintChartStock
gPaint.onPaintChartCrossLine = onPaintChartCrossLine

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(gPaint,hwnd,msg,wParam,lParam)

#创建窗体
createMainWindow(gPaint, "facecat-py", WndProc)

#默认颜色
drawColors = []
drawColors.append("rgb(59,174,218)")
drawColors.append("rgb(185,63,150)")
drawColors.append("rgb(219,68,83)")
drawColors.append("rgb(246,187,67)")
drawColors.append("rgb(216,112,173)")
drawColors.append("rgb(140,192,81)")
drawColors.append("rgb(233,87,62)")
drawColors.append("rgb(150,123,220)")
drawColors.append("rgb(75,137,220)")
drawColors.append("rgb(170,178,189)")

#创建图表
histogram = FCChart()
#填充布局
histogram.dock = "fill"
histogram.leftVScaleWidth = 70
histogram.rightVScaleWidth = 0
histogram.vScaleDistance = 60
histogram.hScalePixel = 50
histogram.hScaleHeight = 30
histogram.candleDivPercent = 1
histogram.volDivPercent = 0
histogram.indDivPercent = 0
histogram.cycle = "tick"
histogram.backColor = "rgb(0,0,0)"
histogram.borderColor = "rgb(100,100,100)"
histogram.textColor = "rgb(255,255,255)"
histogram.scaleColor = "rgb(100,100,100)"
histogram.crossTipColor = "rgb(50,50,50)"
histogram.crossLineColor = "rgb(100,100,100)"
histogram.gridColor = "rgba(100,100,100,125)"
#添加到主框架
addView(histogram, gPaint)

#检查数据
def checkNewData():
	global gPaint
	global histogram
	#创建数据
	data = SecurityData()
	data.close = 0
	data.date = len(histogram.data)
	data.data1 = random.randint(0,100) - 50
	data.data2 = random.randint(0,100) - 50
	data.data3 = random.randint(0,100) - 50
	data.data4 = random.randint(0,100) - 50
	data.data5 = random.randint(0,100) - 50
	data.data6 = random.randint(0,100) - 50
	if data.data1 < 0:
		data.data1 = 0
	if data.data2 < 0:
		data.data2 = 0
	if data.data3 < 0:
		data.data3 = 0
	if data.data4 < 0:
		data.data4 = 0
	if data.data5 < 0:
		data.data5 = 0
	if data.data6 < 0:
		data.data6 = 0
	histogram.data.append(data)
	resetChartVisibleRecord(histogram)
	checkChartLastVisibleIndex(histogram)
	onCalculateChartMaxMin(histogram)
	invalidate(gPaint)
	if gPaint.hWnd != 0:
		threading.Timer(0.02, checkNewData).start()

updateViewDefault(gPaint.views)
checkNewData()
showWindow(gPaint)